package com.xbs.tags;

import java.io.IOException;
import java.text.MessageFormat;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;

/**
 * 自定义分页标签
 * 
 * @author Administrator
 *
 */
public class PageTags extends SimpleTagSupport {

	private int currpage = 1; // 当前页
	private int pagesize = 4; // 页大小
	private int total; // 总记录数
	private int pages; // 总页数
	private String url; // 路径

	public int getCurrpage() {
		return currpage;
	}

	public void setCurrpage(int currpage) {
		this.currpage = currpage;
	}

	public int getPagesize() {
		return pagesize;
	}

	public void setPagesize(int pagesize) {
		this.pagesize = pagesize;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public int getPages() {
		return pages;
	}

	public void setPages(int pages) {
		this.pages = pages;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	@Override
	public void doTag() throws JspException, IOException {
		
		String pattern ="<a href=\"{0}?page={1}&size={2}\">{3}</a>";
		String first=MessageFormat.format(pattern,url,1,pagesize,"第一页");
					
		String last=null;
		if(currpage<=1){
			last="<a href=\"javascript:void(0);\">上一页</a>";
		}else{
			last =MessageFormat.format(pattern,url,currpage-1,pagesize,"上一页");
		}
		
		String next=null;
		if(currpage>=pages){
			next = "<a href=\"javascript:void(0);\">下一页</a>";
		}else{
			next=MessageFormat.format(pattern,url,currpage+1,pagesize,"下一页");
		}
		
		String end = MessageFormat.format(pattern,url,pages,pagesize,"最后一页");
		
		String p = "<div>"+first+" "+last+" "+next+" "+end+"</div>";
		
		this.getJspContext().getOut().print(p);
	}
}
